// let targetHost = "http://192.168.2.197:8081"
let targetHost = "http://129.204.198.223:8080"
// let targetHost = "http://172.23.48.228:8081"
// let targetHost = "http://localhost:8080"

export function GET(url, f){
    let http = create(url, f, 'get');
    http.send();
    return http;
}

export function POSTData(url, data, f){
    let http = create(url, f, 'post');
    http.setRequestHeader("Content-Type", "application/json;charset=UTF-8")
    http.send(JSON.stringify(data));
    return http;
}

export function POST(url, f){
    let http = create(url, f, 'post');
    http.send();
    return http;
}

function create(url, f = (res)=>{}, method){
    let xml = new XMLHttpRequest();
    if(method == 'get'){
        xml.open("GET", targetHost + url);
    }else if(method == 'post'){
        xml.open("POST", targetHost + url);
    }
    xml.onreadystatechange = function (){
        if(xml.readyState == 4){
            if(f != null){
                f(JSON.parse(xml.response));
            }
        }
    }
    let token = sessionStorage.getItem("authorization")
    if(token != undefined && token != ""){
        xml.setRequestHeader("authorization", token);
    }
    return xml;
}

export default {
    GET,
    POST,
    POSTData
}